<template>
    <div>
        <el-drawer
            title=""
            :visible.sync="drawerVisible"
            :size="1200"
            :wrapper-closable="false"
            :with-header="false"
        >
            <div class="drawer-box">
                <div class="handle-btn">
                    <i class="el-icon-close" title="关闭" @click="drawerVisible=false" />
                </div>
                <div class="top-header">
                    <div class="title">
                        <div>
                            <span class="name">{{ info.name }}的分配规则</span>
                        </div>
                        <div class="handle">
                            <el-button size="small" type="primary" plain @click="add">新增规则</el-button>
                        </div>
                    </div>
                </div>
                <div class="content">
                    <!-- 表格 table -->
                    <el-table ref="table" v-loading="loading" :data="dataList" border stripe highlight-current-row>
                        <el-table-column prop="" label="序号" width="60px">
                            <template #default="scope">
                                <span>{{ scope.$index + 1 }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="规则名称">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="detail(scope.row)">{{ scope.row.name }}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column prop="status" label="规则类型">
                            <template #default="scope">
                                <span>{{ scope.row.to_type | typeFilter }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="status" label="规则状态">
                            <template #default="scope">
                                <el-switch v-model="scope.row.status" active-value='1' inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" @change="(e)=>statusChange(e,scope.row)" />
                            </template>
                        </el-table-column>
                        <el-table-column prop="member_name" label="修改人" />
                        <el-table-column prop="updated" label="更新时间" />
                        <el-table-column fixed="right" label="操作" width="160">
                            <template #default="scope">
                                <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </el-drawer>
        <!-- 新建规则 -->
        <add-rule-dialog ref="addRuleDialog" @success="getList" />
    </div>
</template>
<script>
import { ruleList, saveRule, delRule } from '@/api/cluePool'
import AddRuleDialog from './AddRuleDialog'
export default {
    components: {
        AddRuleDialog
    },
    filters: {
        typeFilter(val) {
            const map = {
                1: '池到人',
                2: '池到池'
            }
            return map[val]
        },
        statusFilter(val) {
            const map = {
                1: '启用',
                2: '禁用'
            }
            return map[val]
        }
    },
    data() {
        return {
            loading: false,
            drawerVisible: false,
            id: '',
            info: {},
            dataList: []
        }
    },
    methods: {
        showDrawer(row) {
            if (row.id) {
                this.id = row.id;
                this.info = row;
                this.getList();
                this.drawerVisible = true;
            } else {
                this.$message.error('参数异常！');
            }
            
        },
        getList() {
            this.loading = true;
            ruleList({pool_id: this.id}).then(res => {
                this.dataList = res.data.list;
                this.loading = false;
            })
        },
        add() {
            this.$refs.addRuleDialog.showDialog({pool_id: this.info.id}); 
        },
        detail(item) {
            this.$refs.addRuleDialog.showDialog(item); 
        },
        statusChange(e, item) {
            console.log(e, item);
            item.status = e;
            saveRule(item).then(res => {
                if (res.status == '1') {
                    this.$message.success('状态修改成功！')
                    this.getList();
                } else {
                    this.$message.error(res.message)
                }
            })
        },
        // 删除
        del(id) {
            this.$alert('确定将该规则删除？', '提示', {
                confirmButtonText: '确定',
                showCancelButton: true,
                callback: action => {
                    if (action == 'confirm') {
                        const params = {
                            id
                        }
                        delRule(params).then(res => {
                            if (res.status) {
                                this.getList();
                                this.$message({ type: 'success', message: '操作成功！'});
                            }
                        })
                    }
                }
            });
        }
    }
}
</script>
<style lang="scss" scoped>
::v-deep .el-drawer__body {
    background: transparent;
    overflow: hidden;
    position: relative;
    padding-left: 40px;
    padding-top: 70px;
    box-sizing: border-box;
}
::v-deep .el-drawer {
    background: transparent;
    box-shadow: none;
}
.drawer-box {
    height: 100%;
    background: rgb(242, 245, 251);
    border-radius: 4px 0 0 4px;
    .handle-btn {
        width: 40px;
        // height: 120px;
        padding: 15px 0;
        background: #fff;
        position: absolute;
        top: 110px;
        left: 0;
        border-radius: 4px 0 0 4px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        i {
            height: 30px;
            line-height: 30px;
            &:hover {
                color: #409eff;
            }
        }
    }
}
.top-header {
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
    font-size: 17px;
    color: #666;
    background: #fff;
    .title {
        width: 100%;
        display: flex;
        justify-content: space-between;
        .name {
            font-weight: 600;
            color: #333;
            font-size: 16px;
            margin-right: 30px;
            line-height: 32px;
        }
        .member {
            margin-right: 20px;
            color: #999;
            span {
                color: #333;
            }
        }
    }
    .tags {
        margin-top: 10px;
        .item {
            margin-right: 10px;
        }
    }
    .step {
        margin-top: 10px;
        cursor: pointer;
    }
}
.content {
    padding: 20px;
    background: #fff;
    margin-top: 10px;
    .card {
        background: #fff;
        padding: 5px 15px 10px;
        box-sizing: border-box;
    }
}
.description {
    width: 100%;
    .title {
        font-size: 14px;
        margin: 20px 0 10px;
    }
    .desc-box {
        // display: flex;
        border-top: 1px solid rgba(231, 232, 236, 0.7);
        border-left: 1px solid rgba(231, 232, 236, 0.7);
        // border-collapse: collapse;
        .item {
            font-size: 12px;
            display: flex;
            line-height: 40px;
            .label {
                color: #6c738a;
                background: rgba(247, 248, 250, 0.5);
                border-bottom: 1px solid rgba(231, 232, 236, 0.7);
                border-right: 1px solid rgba(231, 232, 236, 0.7);
                padding: 0 10px;
                min-width: 120px;
            }
            .value {
                display: flex;
                flex: 1;
                align-items: center;
                line-height: 20px;
                padding: 0 10px;
                border-bottom: 1px solid rgba(231, 232, 236, 0.7);
                border-right: 1px solid rgba(231, 232, 236, 0.7);
            }
        }
    }
}
.phone-box {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: rgba(247, 248, 250, 0.5);
    .top {
        display: flex;
        justify-content: space-between;
        font-size: 14px;
        .date {
            color: #999;
        }
    }
    .bottom {
        margin-top: 10px;
    }
}
.follw-list {
    font-size: 12px;
    .top {
        color: #999;
        display: flex;
        justify-content: space-between;
        .handle {
            cursor: pointer;
        }
    }
    .bottom {
        background: #f7f7f9;
        padding: 10px;
        box-sizing: border-box;
        border-radius: 4px;
        margin-top: 10px;
        .file-list {
            width: 100%;
            padding: 10px 0 0;
            box-sizing: border-box;
            display: flex;
            flex-wrap: wrap;
            .img {
                width: 30px;
                height: 30px;
                border-radius: 4px;
                object-fit: cover;
                cursor: pointer;
                position: relative;
                margin-right: 20px;
                margin-top: 10px;
                .del {
                    width: 14px;
                    height: 14px;
                    border-radius: 50%;
                    background: rgba($color: #000, $alpha: 0.5);
                    color: #fff;
                    text-align: center;
                    line-height: 11px;
                    transform: rotate(45deg);
                    position: absolute;
                    top: -4px;
                    right: -4px;
                }
                img {
                    width: 30px;
                    height: 30px;
                    border-radius: 4px;
                }
            }
            .file {
                height: 30px;
                padding: 5px 10px;
                box-sizing: border-box;
                border-radius: 4px;
                background: #f7f7f9;
                cursor: pointer;
                position: relative;
                margin-right: 20px;
                margin-top: 10px;
                img {
                    width: 20px;
                    height: 20px;
                    border-radius: 0;
                    margin-right: 5px;
                    float: left;
                }
            }
        }
    }
}
</style>